package org.rcgwt.client.ui.validator.impl;

import org.rcgwt.client.ui.validator.IClientValidatorContext;
import org.rcgwt.client.ui.validator.IFormatter;
import org.rcgwt.client.ui.validator.ISingleton;
import org.rcgwt.client.ui.validator.IValidatorElement;

/**
 * A credit card number formatter (by 4 groups of 4 digits)
 * 
 * @author Elian ORIOU
 * 
 */

public class CreditCardNumberFormatter implements IFormatter, ISingleton {

	private static final CreditCardNumberFormatter INSTANCE = new CreditCardNumberFormatter();
	private static final int CREDIT_CARD_SPACE_STEP = 4;

	protected CreditCardNumberFormatter() {

	}

	@Override
	public String format(IClientValidatorContext context, String ttf) {
		String s = "";
		int cpt = 0;

		for (int i = 0; i < ttf.length(); i++) {
			s += ttf.charAt(i);
			cpt++;
			if (cpt == CREDIT_CARD_SPACE_STEP && i != ttf.length() - 1) {
				cpt = 0;
				s += " ";
			}
		}
		return s;
	}

	public static IValidatorElement getInstance() {
		return INSTANCE;
	}
}
